﻿using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Chowpoo.Util;
using CourseSystem.Core.BLL;

namespace CourseSystem.Common.Helpers
{
    public class StudentHelper
    {
        public static List<SelectListItem> GetCollege(bool needDef = true)
        {
            var colList = CollegeBLL.GetAll();
            var re = colList.Select(item => new SelectListItem { Value = item.Id.ToString(), Text = item.Name }).ToList();
            if (needDef)
                re.Insert(0, new SelectListItem { Text = "请选择", Value = "0" });
            return re;
        }

        public static List<SelectListItem> GetDepartment(int cid, bool needDef = true)
        {
            var subList = DepartmentBLL.GetByColId(cid);
            var re = subList.Select(item => new SelectListItem { Value = item.Id.ToString(), Text = item.Name }).ToList();
            if (needDef)
                re.Insert(0, new SelectListItem { Text = "请选择", Value = "0" });
            return re;
        }

        public static List<SelectListItem> GetSubject(int cid, bool needDef = true)
        {
            var subList = SubjectBLL.GetByDepId(cid);
            var re = subList.Select(item => new SelectListItem { Value = item.Id.ToString(), Text = item.Name }).ToList();
            if (needDef)
                re.Insert(0, new SelectListItem { Text = "请选择", Value = "0" });
            return re;
        }

        public static List<SelectListItem> GetClass(int sid, bool needDef = true)
        {
            var colList = ClassBLL.GetBySubId(sid);
            var re = colList.Select(item => new SelectListItem { Value = item.Id.ToString(), Text = item.ClassName }).ToList();
            if (needDef)
                re.Insert(0, new SelectListItem { Text = "请选择", Value = "0" });
            return re;
        }

        public static List<SelectListItem> GetGradeYear(bool needDef = true)
        {
            var colList = GradeYearBLL.GetAll();
            var re = colList.Select(item => new SelectListItem { Value = item.Id.ToString(), Text = item.Year.ToString() }).ToList();
            if (needDef)
                re.Insert(0, new SelectListItem { Text = "请选择", Value = "0" });
            return re;
        }

        public static string GenNewUid(int yearId, int subjectId)
        {
            //if (subjectId.ToString().Length > 3)
            //    return null;
            var info = StudentBLL.GetLastStu(yearId, subjectId);
            var num = 0;
            var year = 0;
            if (info == null)
            {
                num = 1;
                year = GradeYearBLL.GetById(yearId).Year;
            }
            else
            {
                year = info.GradeYearInfo.Year;
                var nowNum = GetFromUidNum(info.Uid);
                if (nowNum == -1)
                    return null;
                num = nowNum + 1;
            }
            return year + subjectId.ToString().PadLeft(3, '0') + num.ToString().PadLeft(3, '0');
        }

        private static int GetFromUidNum(string uid)
        {
            var numStr = uid.Substring(7, 3);
            return numStr.CheckInt(-1);
        }
    }
}